package com.hk.housekeeping.controller;

import com.hk.housekeeping.entity.Advertise;
import com.hk.housekeeping.service.AdvertiseService;
import com.hk.housekeeping.utils.PageUtils;
import com.hk.housekeeping.utils.ResponseCode;
import com.hk.housekeeping.utils.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/advertise")
public class AdvertiseController {
    @Resource
    private AdvertiseService advertiseService;

    //增添一条广告信息
    @PostMapping("/add")
    public ResponseEntity<Integer> insert(@RequestBody Advertise advertise){
        ResponseEntity<Integer> entity = null;
        try {
            entity = advertiseService.insert(advertise);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ADVERTISE_ADD_ERROR);
        }
        return entity;
    }

    //删除一条广告信息
    @GetMapping("/delAdvertise")
    public ResponseEntity<Integer> del(@RequestParam int id){
        ResponseEntity<Integer> entity = null;
        try {
            entity = advertiseService.del(id);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ADVERTISE_DEL_ERROR);
        }
        return entity;
    }

    //通过id查询广告信息【信息回显】
    @GetMapping("/list")
    public Advertise list(int id){
        Advertise list = advertiseService.list(id);
        return list;
    }

    //修改广告信息
    @PutMapping("/update")
    public ResponseEntity<Integer> update(@RequestBody Advertise advertise){
        ResponseEntity<Integer> entity = null;
        try {
            entity = advertiseService.update(advertise);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ADVERTISE_UPDATE_ERROR);
        }
        return entity;
    }

    //联合查询 通过广告名称和状态值【0,1,2】查询信息
    @GetMapping("/search")
    public ResponseEntity<PageUtils<List<Advertise>>> selectAdByNameAndStatus(String name,int status,@RequestParam(value = "page", defaultValue = "1") int currentPage, @RequestParam(value = "size", defaultValue = "5") int pageSize){
        ResponseEntity<PageUtils<List<Advertise>>> entity = null;

        //【先判断status是否为2，表示所有状态值都查询】
        if (status == 2) {
            if (StringUtils.isEmpty(name)) {
                //名字为空
                entity = advertiseService.pageSelect(currentPage, pageSize);
            } else {
                //直接按照名字查询，无需管状态值
                entity = advertiseService.selectByName(name, currentPage, pageSize);
            }
        } else {
            if (StringUtils.isEmpty(name) && !StringUtils.isEmpty(status)){
                //广告名称为空，则按状态值查询
                entity = advertiseService.selectByStatus(status, currentPage, pageSize);
            }else if (!StringUtils.isEmpty(name) && !StringUtils.isEmpty(status)){
                //两者不为空（状态值不可能为空，因为接收的类型是int）
                entity = advertiseService.selectByDouble(name, status, currentPage, pageSize);
            }
        }

        return entity;
    }

    //分页查询
    @GetMapping("/pageSelect")
    public ResponseEntity<PageUtils<List<Advertise>>> pageSelect(@RequestParam(value = "page", defaultValue = "1") int currentPage, @RequestParam(value = "size", defaultValue = "5") int pageSize) {
        ResponseEntity<PageUtils<List<Advertise>>> entity = null;
        try {
            entity = advertiseService.pageSelect(currentPage, pageSize);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ADVERTISE_PAGE_ERROR);
        }
        return entity;
    }
}
